import { NCheckbox } from "naive-ui";
import type { CheckboxProps, CheckboxInst } from "naive-ui";
import type { HTMLAttributes } from "vue";
import type { BaseComponentType } from "../types";
import { getSlotsDom } from "../utils";
export type CheckboxFactoryConstructorType = {
  attrs?: HTMLAttributes;
  props?: CheckboxProps;
  defaultSlot?: BaseComponentType;
};
export class CheckboxFactory {
  private attrs: HTMLAttributes = {};
  private props: CheckboxProps = {};
  private defaultSlot: BaseComponentType = null;
  private domRef: CheckboxInst | null = null;
  constructor(data?: CheckboxFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: CheckboxProps) {
    this.props = props;
    return this;
  }
  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }

  focus() {
    this.domRef?.focus();
  }

  blur() {
    this.domRef?.blur();
  }

  create() {
    return (
      <NCheckbox
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as CheckboxInst;
        }}
        {...this.props}
        {...this.attrs}
      >
        {{
          default: () => getSlotsDom(this.defaultSlot),
        }}
      </NCheckbox>
    );
  }
}
